package com.fuyun.rpc.admin.controller;

import com.fuyun.rpc.admin.entity.ServiceInfo;
import com.fuyun.rpc.admin.entity.StatisticsInfo;
import com.fuyun.rpc.admin.service.RegistryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 管理控制台控制器
 */
@Controller
public class AdminController {

	@Autowired
	private RegistryService registryService;

	/**
	 * 首页 - 显示仪表板
	 *
	 * @param model 模型
	 * @return 视图名称
	 */
	@GetMapping("/")
	public String dashboard(Model model) {
		StatisticsInfo statistics = registryService.getStatistics();
		model.addAttribute("statistics", statistics);
		return "dashboard";
	}

	/**
	 * 服务列表页面
	 *
	 * @param serviceName 服务名称（可选）
	 * @param model 模型
	 * @return 视图名称
	 */
	@GetMapping("/services")
	public String services(@RequestParam(required = false) String serviceName, Model model) {
		List<ServiceInfo> services;
		if (serviceName != null && !serviceName.isEmpty()) {
			services = registryService.getServicesByName(serviceName);
		} else {
			services = registryService.getAllServices();
		}
		model.addAttribute("services", services);
		model.addAttribute("searchServiceName", serviceName);
		return "services";
	}

	/**
	 * 服务详情页面
	 *
	 * @param serviceName 服务名称
	 * @param model 模型
	 * @return 视图名称
	 */
	@GetMapping("/service/detail")
	public String serviceDetail(@RequestParam String serviceName, Model model) {
		List<ServiceInfo> services = registryService.getServicesByName(serviceName);
		model.addAttribute("services", services);
		model.addAttribute("serviceName", serviceName);
		return "service-detail";
	}
}